package org.telegram.bot.domain;

import cn.hutool.core.net.URLDecoder;
import cn.hutool.core.net.url.UrlQuery;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.utils.sign.Base64;
import lombok.Data;
import lombok.experimental.Accessors;

import java.io.Serial;
import java.io.Serializable;
import java.nio.charset.StandardCharsets;

/**
 * 入群验证对象 bot_chat_verify
 *
 * @author wyx
 * @date 2024-11-21
 */
@Data
@Accessors(chain = true)
public class BotChatVerifyReq implements Serializable {
    @Serial
    private static final long serialVersionUID = 1L;
    
   private String initData;
   private WebAppInitData webAppInitData = null;
   private String uuid;
   private String code;
    
    public void setInitData(String initData) {
        
        this.initData = new String(URLDecoder.decode(initData.getBytes()));
        if(webAppInitData==null){
            webAppInitData = new WebAppInitData();
            UrlQuery urlQuery = UrlQuery.of(this.initData, StandardCharsets.UTF_8);
            CharSequence userStr = urlQuery.getQueryMap().get("user");
            String str = userStr.toString();
            BotChatVerifyReq.ReqUser user = JSONObject.parseObject(str, BotChatVerifyReq.ReqUser.class);
            webAppInitData.setUser(user);
            webAppInitData.setChat_type(urlQuery.getQueryMap().get("chat_type").toString());
            webAppInitData.setStart_param(
                    JSONObject.parseObject(new String(Base64.decode(urlQuery.getQueryMap().get("start_param").toString())),BotStartApp.class)
            );
        }
    }
    
    @Data
  public class WebAppInitData{
     private BotChatVerifyReq.ReqUser user;
     private String chat_type;
     private BotStartApp start_param;
     private String chat_instance;
     private String auth_data;
     private String signature;
     private String hash;
   }
   
   @Data
  public class ReqUser implements Serializable{
        @Serial
        private static final long serialVersionUID = 1L;
        private Long id;
   }
}
